package com.waji.TableSelectByTCP;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class SelectTableClient extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("等待服务端回复！");
        label.setTranslateY(50);
        Button button = new Button("发送");
        button.setOnAction(event -> {
            try {
                // 客户端向服务端发送
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8001);
                OutputStream outputStream = socket.getOutputStream();
                PrintWriter printWriter = new PrintWriter(outputStream);
                JSONObject root = new JSONObject();
                try {
                    root.put("dbName", "test");
                    root.put("tableName", "test");
                    root.put("columnName", "content");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                printWriter.write(root.toString());
                printWriter.flush();
                socket.shutdownOutput();

                new Thread(() -> {
                    while (true) {
                        String message = "";
                        // 客户端接收服务端的反馈
                        InputStream inputStream;
                        try {
                            inputStream = socket.getInputStream();
                            if(inputStream.available() == 0){
                                continue;
                            }

                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                            String lineMessage;

                            while ((lineMessage = bufferedReader.readLine()) != null) {
                                message += lineMessage;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        String finalMessage = message;
                        Platform.runLater(() -> {
                            if(label.getText().equals("等待服务端回复！")){
                                label.setText("初始值为" + finalMessage);
                            }else{
                                label.setText(label.getText() + "——变化为了"+ finalMessage);
                            }
                        });
                    }
                }).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        primaryStage.setTitle("SelectTableClient");
        Pane pane = new Pane(label, button);
        primaryStage.setScene(new Scene(pane, 400, 200));
        primaryStage.setX(500);
        primaryStage.setY(100);
        primaryStage.show();

        primaryStage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, event -> {
            Platform.exit();
            System.exit(0);
        });
    }
}